package cj.web.admin.controllers;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.admin.dao.ComposeSubjectDAO;
import cj.web.admin.dao.GameDAO;
import cj.web.admin.dao.GradeDAO;
import cj.web.admin.domain.ComposeSubject;
import cj.web.admin.domain.Game;
import cj.web.constants.ComposeSubjectTypeEnum;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/subject")
public class COnlineSubjectController extends BaseController{

	private static Logger logger = Logger.getLogger(COnlineSubjectController.class.getName());
	
	@Autowired
	private GameDAO gameDao;
	@Autowired
	private GradeDAO gradeDao;
	@Autowired
	private ComposeSubjectDAO composeSubjectDao;
	
	@Get("show")
	public String show(Model model,ComposeSubject composeSubject) {
		List<Game> games = gameDao.queryAll();
		List<Map> grades = gradeDao.asGradeEnum();
		
		model.add("Game", games);
		model.add("Grade", grades);
		model.add("MemuEnum", MemuEnum.wlzwtmgl);
		return "/views/online/subjectList.vm";
	}

	@Post("getList")
	public String queryList(ComposeSubject composeSubject) {
		JSONObject jo = new JSONObject();
		
		List<ComposeSubject> objs =  composeSubjectDao.query(composeSubject);
		
		for(ComposeSubject obj:objs){
			obj.setTypeName(ComposeSubjectTypeEnum.getName(obj.getType().toString()));
		}
		
		int size = composeSubjectDao.querySize(composeSubject);
		
		jo.put("data", objs);
		jo.put("draw", composeSubject.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		List<Game> games = gameDao.queryAll();
		List<Map> grades = gradeDao.asGradeEnum();
		
		model.add("Game", games);
		model.add("Grade", grades);
		model.add("MemuEnum", MemuEnum.wlzwtmgl);
		model.add("SubjectTypeEnum", EnumSet.allOf(ComposeSubjectTypeEnum.class));
		return "/views/online/subjectDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,@Param("gameId")int gameId,@Param("gradeId")int gradeId,@Param("type")int type,
			@Param("subjectNo")List<Integer> subjectNo,@Param("title")List<String> title,@Param("content")List<String> content,
			HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String account = returnSessionObj(request).getAccount();
		
		try {
			for(int i = 0; i < subjectNo.size(); i++){
				composeSubjectDao.create(gameId, gradeId, subjectNo.get(i).intValue(), type, title.get(i), content.get(i), account);
			}
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 新建操作界面
	 * @param model
	 * @return
	 */
	@Get("createSinglePage")
	public String createSinglePage(Model model)
	{
		List<Game> games = gameDao.queryAll();
		List<Map> grades = gradeDao.asGradeEnum();
		
		model.add("Game", games);
		model.add("Grade", grades);
		model.add("MemuEnum", MemuEnum.wlzwtmgl);
		model.add("SubjectTypeEnum", EnumSet.allOf(ComposeSubjectTypeEnum.class));
		return "/views/online/subjectSingleDetail.vm";
	}
	
	@Post("createSingle")
	public String createSingle(Model model,@Param("gameId")int gameId,@Param("gradeIds")String gradeIds,@Param("type")int type,
			@Param("subjectNo")int subjectNo,@Param("title")String title,@Param("content")String content,
			HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String account = returnSessionObj(request).getAccount();
		
		try {
			String[] gradeId = gradeIds.split(",");
			for(int i = 0; i < gradeId.length; i++){
				composeSubjectDao.create(gameId, Integer.parseInt(gradeId[i]), subjectNo, type, title, content, account);
			}
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		List<Game> games = gameDao.queryAll();
		List<Map> grades = gradeDao.asGradeEnum();
		
		model.add("Game", games);
		model.add("Grade", grades);
		
		ComposeSubject obj = composeSubjectDao.queryById(id);
		model.add("obj", obj);
		model.add("MemuEnum", MemuEnum.wlzwtmgl);
		model.add("SubjectTypeEnum", EnumSet.allOf(ComposeSubjectTypeEnum.class));
		return "/views/online/subjectEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,ComposeSubject composeSubject,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String account = returnSessionObj(request).getAccount();
		composeSubject.setUpdateUser(account);
		
		try {
			composeSubjectDao.update(composeSubject);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		JSONObject jo = new JSONObject();
		
		composeSubjectDao.deleteById(id);
		return "@"+jo.toString();
	}
}
